#include "CVistaThreadPool.hpp"
#include <stdio.h>

CVistaThreadPool::CVistaThreadPool(DWORD nMinThreads,DWORD nMaxThreads)
{
    
    InitializeThreadpoolEnvironment(&m_TPENV);
    m_pool=CreateThreadpool(NULL);

    if (m_pool!=NULL)
    {
        //Assoicate the environment with the thread pool   
        SetThreadpoolCallbackPool(&m_TPENV,m_pool);

        //Set min/max thread
        m_MinThreads= nMinThreads < 0 ? ProcessorCount() :nMinThreads;
        m_MaxThreads=nMaxThreads < m_MinThreads ? m_MinThreads : nMaxThreads;
        SetThreadpoolThreadMaximum(m_pool,m_MaxThreads);
        BOOL bRet=SetThreadpoolThreadMinimum(m_pool,m_MinThreads);
        if (FALSE == bRet) 
            printf("SetThreadpoolThreadMinimum failed.");
    }
}



DWORD CVistaThreadPool::ProcessorCount()
{
    SYSTEM_INFO systemInfo;
    ::GetSystemInfo(&systemInfo);
    return systemInfo.dwNumberOfProcessors;
}

CVistaThreadPool::~CVistaThreadPool()
{
    DestroyThreadpoolEnvironment(&m_TPENV);
    if (m_pool!=NULL)
        CloseThreadpool(m_pool);
}




